const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path');
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();
//点击确定修改
    router.post('/updatefouramB',function (req,res,next) {
      if(req.files.length==0)
      {
        db.query('UPDATE forumbinfo SET forumBName="'+req.body.fouramBname+'" ,userId='+parseInt(req.body.forumA)+',forumBUp='+parseInt(req.body.forumBUp)+',forumAId='+parseInt(req.body.forumA)+' WHERE forumBId='+parseInt(req.body.fourBmId),function (err,data) {
          if(err){
            res.status(400).send('<script>alert("修改执行失败，请稍后再次执行!");window.location.href="404.html";</script>').end();
          }
          else{
            if(data.length==0){
              res.status(400).send('<script>alert("修改失败!");window.location.href="404.html";</script>').end();
            }
            else{
              res.status(200).send('<script>alert("修改成功!");window.location.href="forumbinfo.html";</script>')
            }
          }
        })
      }
      else {
        var ext = pathLib.parse(req.files[0].originalname).ext;
        //console.log("dxcvbnm" + ext);
        //文件旧存放路径
        var oldPath = req.files[0].path;
        // console.log("文件旧存放路径"+oldPath)
        //文件新存放路径
        var newPath = req.files[0].path + ext;
        // console.log("文件新存放路径" + newPath)
        //文件新名
        var newFileName = req.files[0].filename + ext;

        console.log("我上的图片" + newFileName);
        fs.rename(oldPath, newPath, function (err) {
          if (err) {
            res.status(500).send('upload is error');
          }

          db.query('UPDATE forumbinfo SET forumBName="' + req.body.fouramBname + '" ,userId=' + parseInt(req.body.forumA) + ',forumbPic="' + newFileName + '",forumBUp=' + parseInt(req.body.forumBUp) + ',forumAId=' + parseInt(req.body.forumA) + ' WHERE forumBId=' + parseInt(req.body.fourBmId), function (err, data) {
            if (err) {
              res.status(400).send('<script>alert("修改执行失败，请稍后再次执行!");window.location.href="404.html";</script>').end();
            }
            else {
              if (data.length == 0) {
                res.status(400).send('<script>alert("修改失败!");window.location.href="404.html";</script>').end();
              }
              else {
                res.status(200).send('<script>alert("修改成功!");window.location.href="forumbinfo.html";</script>')
              }
            }
          });

        });
      }
    });
      //开始进行加载数据到修改页面上
    router.use('/updateforumbinfo.html',function (req,res,next) {
        db.query('SELECT * FROM forumainfo',function (err,data) {
            if(err){
                res.send('<script>alert("程序错误");window.location.href="400.html";</script>')
            }else{
                if(data.length==0) {
                    res.send('<script>alert("暂无您所需的数据");window.location.href="500.html";</script>');
                }
                else{
                    req.forumA=data;
                }
            }

        });

        db.query('select *from userinfo',function (err,data) {
            if(err){
                res.send('<script>alert("程序错误");window.location.href="400.html";</script>')
            }else{
                if(data.length==0) {
                    res.send('<script>alert("暂无您所需的数据");window.location.href="500.html";</script>');
                }
                else{
                    req.user=data;
                }
            }
        });
        db.query('SELECT * FROM forumbinfo,forumainfo,userinfo where userinfo.userId=forumbinfo.userId and forumbinfo.forumAId=forumainfo.forumAId and forumBId='+parseInt(req.query.forumBId),function (err,data) {
            if(err){
                res.send('<script>alert("程序出错");window.location.href="400.html";</script>').end();
            }
            else{
                if(data.length==0){
                    res.send('<script>alert("暂无数据");window.location.href="400.html";</script>').end();
                }
                else{
                    res.render("forum/updateforumbinfo.ejs",{msg:data,forumA:req.forumA,user: req.user});
                }
            }
        });
    });

    return router;
}
